/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.panels;

import nl.biz_e.gwt.baselib.client.i18n.Constants;
import nl.biz_e.gwt.baselib.client.i18n.GwtBaselibConstants;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * TODO: Write type comment
 * 
 * @author Joost A. Bloemsma
 */
public class StdLoginPanel extends VerticalPanel implements KeyPressHandler {
    private final GwtBaselibConstants constants = Constants.getBaseLib();
    private final TextBox name = new TextBox();
    private final PasswordTextBox pwd = new PasswordTextBox();
    private final Button button = new Button(constants.logIn());
    private static final char ENTER_KEY = (char) 13;

    /**
     * Constructs a new StdLoginPanel instance
     * 
     * @param handler
     */
    public StdLoginPanel(ClickHandler handler) {
        this("", handler);
    }

    /**
     * Constructs a new StdLoginPanel instance
     * 
     * @param msg
     * @param handler
     */
    public StdLoginPanel(String msg, ClickHandler handler) {
        super();
        name.addKeyPressHandler(this);
        pwd.addKeyPressHandler(this);
        this.setSpacing(5);
        this.setStyleName("loginpanel");
        if (msg != null) {
            if (msg.length() > 0) {
                add(new Label(msg));
            }
        }

        Grid g = new Grid(2, 2);
        g.setCellSpacing(0);
        g.setWidget(0, 0, new Label(constants.name() + ": "));
        g.setWidget(0, 1, name);
        g.setWidget(1, 0, new Label(constants.pwd() + ": "));
        g.setWidget(1, 1, pwd);

        this.add(g);
        button.addClickHandler(handler);
        this.add(button);
        this.setCellHorizontalAlignment(button,
                HasHorizontalAlignment.ALIGN_RIGHT);
    }

    /**
     * @return the login name
     */
    public String getName() {
        return name.getText();
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return pwd.getText();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onKeyPress(KeyPressEvent event) {
        if (event.getCharCode() == ENTER_KEY) {
            button.click();
        }
    }
}
